package com.ssmfuse.service.impl;

import com.ssmfuse.Bean.Page;
import com.ssmfuse.domain.Question;
import com.ssmfuse.mapper.QuestionMapper;
import com.ssmfuse.service.QuestionService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("QuestionService")
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper QuestionDao;

    @Override
    public void addQuestion(Question question) {
        QuestionDao.addQuestion(question);
    }

    @Override
    public void deleteQuestion(int id) {
        QuestionDao.deleteQuestion(id);
    }

    @Override
    public void updateQuestion(Question question) {
        QuestionDao.updateQuestion(question);
    }


    @Override
    public List<Question> findQuestion(String questionName1,String questionName2,String questionName3) {
        List<Question> list = QuestionDao.findQuestion("%" + questionName1 + "%","%" + questionName2 + "%","%" + questionName3 + "%");
        return list;
    }

    @Override
    public List<Question> questionList() {
        List<Question> list = QuestionDao.questionList();
        return list;
    }

    @Override
    public List<Question> findQuestion_two(String questionName1, String questionName2) {
        List<Question> list = QuestionDao.findQuestion_two("%" + questionName1 + "%","%" + questionName2 + "%");
        return list;
    }

    @Override
    public List<Question> findQuestion_one(String questionName1) {
        List<Question> list = QuestionDao.findQuestion_one("%" + questionName1 + "%");
        return list;
    }

    @Override
    public List<Question> findQuestion_five(String questionName1, String questionName2, String questionName3, String questionName4, String questionName5) {
        List<Question> list = QuestionDao.findQuestion_five("%" + questionName1 + "%","%" + questionName2 + "%","%" + questionName3 + "%","%" + questionName4 + "%","%" + questionName5 + "%");
        return list;
    }

    @Override
    public Page<Question> page(int pageNo, int pageSize) {
        Page<Question> page = new Page<>();

        //设置每页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount = QuestionDao.queryForPageTotal();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        int pageTotal = pageTotalCount / pageSize;
        if( pageTotalCount % pageSize > 0 )
        {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);

        /**
         * 数据边界的有效检查
         */
        if( pageNo < 1 ) pageNo = 1;
        if( pageNo > pageTotal ){
            pageNo = pageTotal;
        }

        //设置当前页码
        page.setPageNo(pageNo);

        //求当前页数据的开始索引
        int begin = ( page.getPageNo() -1 ) * pageSize;
        //求当前页数据
        List<Question> items = QuestionDao.queryForPageItems(begin,pageSize);
        //设置当前页数据
        page.setItems(items);

        return page;
    }

    @Override
    public Integer queryForPageTotal() {
        return QuestionDao.queryForPageTotal();
    }

    @Override
    public List<Question> queryForPageItems(int begin, int pageSize) {
        List<Question> items = QuestionDao.queryForPageItems(begin, pageSize);
        return items;
    }

}
